package org.xq.softcup.mq.remoting.invoker.reference.impl;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.xq.softcup.mq.remoting.invoker.XQRpcInvokerFactory;
import org.xq.softcup.mq.remoting.invoker.call.CallType;
import org.xq.softcup.mq.remoting.invoker.call.XQRpcInvokeCallback;
import org.xq.softcup.mq.remoting.invoker.reference.XQRpcReferenceBean;
import org.xq.softcup.mq.remoting.invoker.router.LoadBalance;
import org.xq.softcup.mq.remoting.net.NetEnum;
import org.xq.softcup.mq.serialize.Serializer;

public class XQRpcSpringReferenceBean implements FactoryBean<Object>, InitializingBean {
    private String netType = NetEnum.NETTY.name();
    private String serialize = Serializer.SerializeEnum.HESSIAN.name();
    private String callType = CallType.SYNC.name();
    private String loadBalance = LoadBalance.ROUND.name();
    private Class<?> iface;
    private String version;
    private long timeout = 1000L;
    private String address;
    private String accessToken;
    private XQRpcInvokeCallback invokeCallback;
    private XQRpcInvokerFactory xqRpcInvokerFactory;
    private XQRpcReferenceBean xqRpcReferenceBean;

    public void setNetType(String netType) {
        this.netType = netType;
    }

    public void setSerialize(String serialize) {
        this.serialize = serialize;
    }

    public void setCallType(String callType) {
        this.callType = callType;
    }

    public void setLoadBalance(String loadBalance) {
        this.loadBalance = loadBalance;
    }

    public void setIface(Class<?> iface) {
        this.iface = iface;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public void setInvokeCallback(XQRpcInvokeCallback invokeCallback) {
        this.invokeCallback = invokeCallback;
    }

    public void setXqRpcInvokerFactory(XQRpcInvokerFactory xqRpcInvokerFactory) {
        this.xqRpcInvokerFactory = xqRpcInvokerFactory;
    }

    private void init() {
        NetEnum netTypeEnum = NetEnum.autoMatch(this.netType, null);
        Serializer.SerializeEnum serializeEnum = Serializer.SerializeEnum.match(this.serialize, null);
        Serializer serializer = serializeEnum != null ? serializeEnum.getSerializer() : null;
        CallType callTypeEnum = CallType.match(this.callType, null);
        LoadBalance loadBalanceEnum = LoadBalance.match(this.loadBalance, null);

        this.xqRpcReferenceBean = new XQRpcReferenceBean(netTypeEnum, serializer, callTypeEnum, loadBalanceEnum,
                this.iface, this.version, this.timeout, this.address, this.accessToken,
                this.invokeCallback, this.xqRpcInvokerFactory);
    }

    public void afterPropertiesSet() {
        init();
    }

    public Object getObject() {
        return this.xqRpcReferenceBean.getObject();
    }

    public Class<?> getObjectType() {
        return this.xqRpcReferenceBean.getObjectType();
    }

    public boolean isSingleton() {
        return false;
    }
}